package cn.zhangchg.meituan;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;

public class Test {


    public static void main(String[] args) {
        //test1();
        //test2();
        test3();
    }

    private static void test1() {
        AService aService = new AService();
        BService bService = new BService();
        bService.setAService(aService);
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            ids.add(i);
        }
        List<Integer> integers = bService.get(ids);
        System.out.println(JSON.toJSONString(integers));
    }

    private static void test2() {
        AService aService = new AService();
        BService bService = new BService();
        CService cService = new CService();
        List<Long> ids = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ids.add((long) i);
        }
        Map<Long, List<Long>> parnterMap = aService.getHotelByParnterId(ids);
        List<Long> hotelAllIds = new ArrayList<>();
        parnterMap.values().forEach(hotelAllIds::addAll);
        List<Hotel> hotels = bService.batchGetHotelByPoiId(hotelAllIds);
        List<Contact> contacts = cService.batchGetContactBypoiIds(hotelAllIds);
        Map<Long, Hotel> hotelMap = hotels.stream().collect(Collectors.toMap(Hotel::getPoiID, a -> a, (a, b) -> a));
        Map<Long, Contact> contactMap = contacts.stream().collect(Collectors.toMap(Contact::getPoiID, a -> a, (a, b) -> a));

        List<ParnterInfo> parnterInfos = parnterMap.keySet().stream().map(parnterId -> {
            List<Long> hotelIds = parnterMap.get(parnterId);
            List<HotelInfo> hotelInfos = hotelIds.stream().map(hotelId -> {
                Hotel hotel = hotelMap.get(hotelId);
                Contact contact = contactMap.get(hotelId);
                return new HotelInfo(hotel == null ? null : hotel.getHotelName(), contact == null ? null : contact.getContactName());
            }).collect(Collectors.toList());
            return new ParnterInfo(parnterId, hotelInfos);
        }).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(parnterInfos));
    }

    private static void test3()  {
        AService aService = new AService();
        BService bService = new BService();
        CService cService = new CService();
        Callable<Integer> callableA = aService::get;
        Callable<Integer> callableB = bService::get;
        Callable<Integer> callableC = cService::get;
        FutureTask<Integer> futureTaskA = new FutureTask<>(callableA);
        FutureTask<Integer> futureTaskB = new FutureTask<>(callableB);
        FutureTask<Integer> futureTaskC = new FutureTask<>(callableC);
        new Thread(futureTaskA).start();
        new Thread(futureTaskB).start();
        new Thread(futureTaskC).start();
        Integer a1;
        Integer b1;
        Integer c1;
        try {
            a1 = futureTaskA.get();
            b1 = futureTaskB.get();
            c1 = futureTaskC.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println(a1);
        System.out.println(b1);
        System.out.println(c1);
        System.err.println(a1 + b1+ c1);
    }
}
